// Learn TypeScript:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {



    @property({
        displayName: "重力",
        tooltip: '自身重量，跳起的力量不变，值越大跳的越低，落下的越快，格斗游戏一般5以上'
    })
    gravityMax: number = 5;

    @property({
        displayName: "矩形框",
        tooltip: '自身的碰撞约束框'
    })
    Rect: cc.Rect = new cc.Rect(0, 0, 32, 50);
   

    // onLoad () {}
    public isJump() {
        let cy = this.Rect.height, tw = this.Rect.width / 2;//

        this.node.x = Math.round(this.node.x);
        this.node.y = Math.round(this.node.y);
        let r = new cc.Rect(this.node.x + window['flagPoint'].x - tw, window['flagPoint'].y + this.node.y + cy, this.Rect.width, cy + 2);
        let pw = 100;
        let ph = 100;
        let nodeX = r.x;
        let nodeY = r.y;
        let tx = (Math.floor(nodeX / pw));
        let ty = (Math.ceil(nodeY / ph));
        let iw = Math.floor(((r.width + nodeX - 1) / pw));
        let ih = Math.ceil((nodeY - r.height + 1) / ph);
        for (let xt = tx; xt <= iw; xt++) {
            for (let yt = ty; yt >= ih; yt--) {
                let xn = (xt * pw).toString();
                let yn = (yt * ph).toString();
                if (window['hitObj'] != null && window['hitObj'][xn] != null && window['hitObj'][xn][yn] != null) {
                    for (let i = 0; i < window['hitObj'][xn][yn].length; i++) {
                        let r1 = new cc.Rect(this.node.x + window['flagPoint'].x - tw, (window['flagPoint'].y + this.node.y) * -1, this.Rect.width, 2);

                        let r2 = window['hitObj'][xn][yn][i];
                        if (!((r1.x + r1.width) <= r2.x || (r2.x + r2.width) <= r1.x || (r1.y + r1.height) <= r2.y || (r2.y + r2.height) <= r1.y)) {//相交
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    }
    public contains(x, y) {

        let pw = 100;
        let ph = 100;
        let nodeX = x+ window['flagPoint'].x;
        let nodeY = y+window['flagPoint'].y;
        let tx = (Math.floor(nodeX / pw));
        let ty = (Math.ceil(nodeY / ph));
        let xn = (tx * pw).toString();
        let yn = (ty * ph).toString();
        if (window['hitObj'] != null && window['hitObj'][xn] != null && window['hitObj'][xn][yn] != null) {
            for (let i = 0; i < window['hitObj'][xn][yn].length; i++) {
                let r2 = new cc.Rect(window['hitObj'][xn][yn][i].x,window['hitObj'][xn][yn][i].y,window['hitObj'][xn][yn][i].width,window['hitObj'][xn][yn][i].height);
                if (r2.contains(new cc.Vec2(nodeX,nodeY*-1))){
                    return true;
                }
            }
        }

        return false;
    }
    public hitHeadFun: Function;
    public hitWallFun: Function;
    private hitRect() {
        let cy = this.Rect.height;
        let bo = false, tw = this.Rect.width / 2;
        this.node.x = Math.round(this.node.x);
        this.node.y = Math.round(this.node.y);
        let r = new cc.Rect(this.node.x + window['flagPoint'].x - tw, window['flagPoint'].y + this.node.y + cy, this.Rect.width, cy);
        let pw = 100;
        let ph = 100;
        let nodeX = r.x;
        let nodeY = r.y;
        let tx = (Math.floor(nodeX / pw));
        let ty = (Math.ceil(nodeY / ph));
        let iw = Math.floor(((r.width + nodeX - 1) / pw));
        let ih = Math.ceil((nodeY - r.height + 1) / ph);
        //console.log(window['hitObj'][8800],window['hitObj'][8800][-22900]);
        for (let xt = tx; xt <= iw; xt++) {
            for (let yt = ty; yt >= ih; yt--) {
                let xn = (xt * pw).toString();
                let yn = (yt * ph).toString();
                if (window['hitObj'] != null && window['hitObj'][xn] != null && window['hitObj'][xn][yn] != null) {
                    for (let i = 0; i < window['hitObj'][xn][yn].length; i++) {
                        let r1 = new cc.Rect(this.node.x + window['flagPoint'].x - tw, (window['flagPoint'].y + this.node.y) * -1 - cy, this.Rect.width, cy);
                        let r2 = window['hitObj'][xn][yn][i];

                        if (!((r1.x + r1.width) <= r2.x || (r2.x + r2.width) <= r1.x || (r1.y + r1.height) <= r2.y || (r2.y + r2.height) <= r1.y)) {

                            let num = 0;
                            if (this.pverLocation != null) {
                                num = Math.abs(this.node.x - this.pverLocation.x) + Math.abs(this.node.y - this.pverLocation.y);
                            }
                            if (this.pverLocation == null || num == 0) {
                                this.bounce = 0;
                                this.node.y = window['hitObj'][xn][yn][0].y * -1;
                            } else {
                                let nx = (this.node.x - this.pverLocation.x) / num;
                                let ny = (this.node.y - this.pverLocation.y) / num;
                                if (ny == 1) {
                                    if (r1.y > r2.y) {
                                        this.bounce = 0;
                                        if (this.hitHeadFun != null) {
                                            this.hitHeadFun(r2['id']);//碰到头了
                                        }
                                    }
                                } else if (ny == -1) {
                                    this.bounce = 0;
                                    bo = true;
                                }
                                if (nx) {
                                    if (this.hitWallFun != null) {
                                        this.hitWallFun(r2['id'], nx);//碰到墙了
                                    }
                                }
                                let nr = r1.intersection(new cc.Rect(), r2);
                                let k = 0;
                                this.node.x -= nx * nr.width;
                                this.node.y -= ny * nr.height;
                            }

                        }
                    }
                }
            }
        }
        this.pverLocation = null;

    }

    start() {

    }
    public bounce: number = 0;
    public pzbo = false;
    private ptArr: Array<any> = [];
    private pverLocation = null;
    public setXY(x = null, y = null) {
        let bo: boolean = false;
        let p = { "x": this.node.x, "y": this.node.y };
        if (y !== null) {
            //let ny=Math.abs(this.node.y-y);
            this.node.y = y;
            bo = true;
        }
        if (x !== null) {
            this.node.x = x;
            bo = true;
        }
        if (bo) {
            this.pverLocation = p;
            this.hitRect();
            if (this.updateMapLocation != null && (this.node.x != p.x || this.node.y != p.y)) {
                this.updateMapLocation();
            }

        }
    }
    public updateMapLocation: Function;
    private zhongli: number = 0;
    private onWorld() {
        let i = this.gravityMax - this.bounce;
        //  if(this.zhongli>this.zhongliMax){this.zhongli=this.zhongliMax;}
        if (i > 16) { i = 16; }
        this.setXY(null, this.node.y - i);
        this.bounce -= this.gravityMax / 7;

    }
    isDown(){
       return this.bounce<this.gravityMax;
    }
    update(dt) {
        if (!window['runBo']) { return; }
        //   this.pverLocation=null;
        this.onWorld();
        //   this.hitRect();
    }
}
